import { drawStrokeRect, drawFillRect, drawCircle, drawFillCircle } from '../utils/draw.js';
import { generateUUID } from '../utils/tool.js';

/** 基础绘制对象类 */
export default class Actor {
    // 需要注意的是，如果是圆类，则width和height不起作用，需要使用radius参数
    constructor(ctx, x, y, width, height, type = 'circle', radius = 30, color = 'red') {
        this.id = generateUUID();
        this.ctx = ctx;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
        this.type = type;
        this.radius = radius;
    }

    /** 是否到达地图左右边界,有时候需要预测下一步是否到达地图边界，所以可以传入下一步计算的值 */
    isTouchingMapWallX(nextX) {
        // 矩形类和圆类分别判断
        let tempX = nextX;
        if (['strokeRect', 'fillRect'].includes(this.type)) {
            return {
                left: tempX < 0,
                right: tempX + this.width > window.GameData.canvas.width,
            };
        } else {
            return {
                left: tempX - this.radius < 0,
                right: tempX + this.radius > window.GameData.canvas.width,
            };
        }
    }

    /** 是否到达地图上下边界 */
    isTouchingMapWallY(nextY) {
        // 矩形类和圆类分别判断
        let tempy = nextY;
        if (['strokeRect', 'fillRect'].includes(this.type)) {
            return {
                top: tempy < 0,
                bottom: tempy + this.height > window.GameData.canvas.height,
            };
        } else {
            return {
                top: tempy - this.radius < 0,
                bottom: tempy + this.radius > window.GameData.canvas.height,
            };
        }
    }

    update() {}

    draw() {
        switch (this.type) {
            case 'strokeRect':
                drawStrokeRect(this.ctx, this.x, this.y, this.width, this.height, this.color);
                break;
            case 'fillRect':
                drawFillRect(this.ctx, this.x, this.y, this.width, this.height, this.color);
                break;
            case 'circle':
                drawCircle(this.ctx, this.x, this.y, this.radius, this.color);
                break;
            case 'fillCircle':
                drawFillCircle(this.ctx, this.x, this.y, this.radius, this.color);
                break;

            default:
                break;
        }
    }
}
